/*
 * GraphicsManager.java
 *
 * Created on 26 de Dezembro de 2006, 3:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Managers;

import MIDlet.FlunkerCanvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.World;

/**
 *
 * @author JAKE
 */
public class GraphicsManager {
    
    private int viewport_x;
    private int viewport_y;
    private int viewport_width;
    private int viewport_height;
    
    private FlunkerManager flunker_manager;
    private FlunkerCanvas flunker_canvas;
    
    private Graphics3D flunker_3D;
    
    // Rendering hints
    public static final int STRONG_RENDERING_HINTS = Graphics3D.ANTIALIAS | Graphics3D.TRUE_COLOR | Graphics3D.DITHER;
    public static final int WEAK_RENDERING_HINTS = 0;
    public static int RENDERING_HINTS = STRONG_RENDERING_HINTS; //hardcoded to strong, create a decision function for this
    
    /** Creates a new instance of GraphicsManager */
    public GraphicsManager(FlunkerManager manager) {
        flunker_manager = manager;
        flunker_canvas = flunker_manager.getCanvas();
        flunker_3D = Graphics3D.getInstance();
    }
    
    public Graphics3D getGraphics3D(){
        return flunker_3D;
    }
    
    public void render(){
        flunker_canvas.repaint(viewport_x, viewport_y, viewport_width, viewport_height);
    }
    
    
    /**
     * Make sure that the content is rendered with the correct aspect ratio.
     */
    void setupAspectRatio() {
        viewport_x = 0;
        viewport_y = 0;
        viewport_width = flunker_canvas.getWidth();
        viewport_height = flunker_canvas.getHeight();
        
        Camera cam = flunker_manager.getWorld().getActiveCamera();
        
        float[] params = new float[4];
        int type = cam.getProjection(params);
        if(type != Camera.GENERIC && cam != null) {
            //calculate window aspect ratio
            float waspect=viewport_width/viewport_height;
            
            if (waspect<params[1]) {
                float height = viewport_width/params[1];
                viewport_height=(int)height;
                viewport_y=(flunker_canvas.getHeight()-viewport_height)/2;
            } else {
                float width = viewport_height*params[1];
                viewport_width=(int)width;
                viewport_x=(flunker_canvas.getWidth()-viewport_width)/2;
            }
        }
    }
    
    
    public void paint(Graphics g) {
               
        // clear any area of the screen that is not drawn to by m3g
        if(g.getClipWidth() != viewport_width ||
                g.getClipHeight() != viewport_height ||
                g.getClipX() != viewport_x ||
                g.getClipY() != viewport_y) {
            g.setColor(0x00);
            g.fillRect(0, 0, flunker_canvas.getWidth(), flunker_canvas.getHeight());
        }
        
        //get the 3D scene from the main manager
        World flunker_world = flunker_manager.getWorld();
        
        // render the 3D scene
        if ((flunker_3D != null) && (flunker_world != null)) {
            flunker_3D.bindTarget(g, true, RENDERING_HINTS);
            flunker_3D.setViewport(viewport_x, viewport_y, viewport_width, viewport_height);
            flunker_3D.render(flunker_world);
            flunker_3D.releaseTarget();
        }
    }
    
}
